<template>
  <div class="table-box">
    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="物料编码" prop="materialCode">
        <el-input v-model="searchForm.materialCode" placeholder="请输入物料编码" clearable />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="searchForm.materialName" placeholder="请输入物料名称" clearable />
      </el-form-item>
      <el-form-item label="仓库" prop="warehouse">
        <el-select v-model="searchForm.warehouse" placeholder="请选择仓库" clearable style="width: 180px">
          <el-option label="原料仓" value="RAW_MATERIAL" />
          <el-option label="辅料仓" value="AUXILIARY" />
          <el-option label="包材仓" value="PACKAGING" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option label="正常" value="NORMAL" />
          <el-option label="预警" value="WARNING" />
          <el-option label="缺货" value="OUT_OF_STOCK" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleAdd">库存调整</el-button>
      <el-button type="success" :icon="Download" @click="handleExport">导出</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="materialCode" label="物料编码" />
      <el-table-column prop="materialName" label="物料名称" />
      <el-table-column prop="specification" label="规格型号" />
      <el-table-column prop="unit" label="单位" />
      <el-table-column prop="warehouse" label="仓库">
        <template #default="scope">
          <el-tag>{{ getWarehouseText(scope.row.warehouse) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="currentStock" label="当前库存" align="right">
        <template #default="scope">
          <span :class="getStockClass(scope.row)">{{ scope.row.currentStock }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="safetyStock" label="安全库存" align="right" />
      <el-table-column prop="maxStock" label="最大库存" align="right" />
      <el-table-column prop="status" label="库存状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="lastUpdateTime" label="最后更新时间" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">查看详情</el-button>
          <el-button type="warning" link @click="handleAdjust(scope.row)">库存调整</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="rawMaterialInventory">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { CirclePlus, Download } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 查询表单
const searchForm = reactive({
  materialCode: "",
  materialName: "",
  warehouse: "",
  status: ""
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取仓库文本
const getWarehouseText = (warehouse: string) => {
  const warehouseMap: Record<string, string> = {
    RAW_MATERIAL: "原料仓",
    AUXILIARY: "辅料仓",
    PACKAGING: "包材仓"
  };
  return warehouseMap[warehouse] || warehouse;
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    NORMAL: "success",
    WARNING: "warning",
    OUT_OF_STOCK: "danger"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    NORMAL: "正常",
    WARNING: "预警",
    OUT_OF_STOCK: "缺货"
  };
  return statusMap[status] || status;
};

// 获取库存样式
const getStockClass = (row: any) => {
  if (row.currentStock <= 0) return "stock-danger";
  if (row.currentStock <= row.safetyStock) return "stock-warning";
  return "stock-normal";
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // 模拟数据
    const mockData = [
      {
        id: 1,
        materialCode: "RM001",
        materialName: "钢材A",
        specification: "Q235 10*20mm",
        unit: "kg",
        warehouse: "RAW_MATERIAL",
        currentStock: 1500,
        safetyStock: 1000,
        maxStock: 5000,
        status: "NORMAL",
        lastUpdateTime: "2024-01-15 10:30:00"
      },
      {
        id: 2,
        materialCode: "RM002",
        materialName: "塑料颗粒B",
        specification: "PP 白色",
        unit: "kg",
        warehouse: "RAW_MATERIAL",
        currentStock: 800,
        safetyStock: 1000,
        maxStock: 3000,
        status: "WARNING",
        lastUpdateTime: "2024-01-15 09:15:00"
      }
    ];
    
    tableData.value = mockData;
    pagination.total = mockData.length;
  } catch (error) {
    console.error("获取原材料库存失败:", error);
    ElMessage.error("获取原材料库存失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 新增库存调整
const handleAdd = () => {
  ElMessage.info("库存调整功能开发中");
};

// 导出
const handleExport = () => {
  ElMessage.info("导出功能开发中");
};

// 查看详情
const handleView = (row: any) => {
  ElMessage.info(`查看 ${row.materialName} 详情`);
};

// 库存调整
const handleAdjust = (row: any) => {
  ElMessage.info(`调整 ${row.materialName} 库存`);
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

.stock-normal {
  color: #67c23a;
  font-weight: bold;
}

.stock-warning {
  color: #e6a23c;
  font-weight: bold;
}

.stock-danger {
  color: #f56c6c;
  font-weight: bold;
}
</style>